/*:
* @plugindesc 枫之舞界面 - 窗口基本（基本颜色、字体、绘制等）
* @author 老饕
* @version 1.0.0
* @date 2020.1.14
*/

let SWDA_WindowBase = {};
// ========================================================================
// 基本颜色
// ========================================================================
Window_Base.prototype.normalLightColor = function () {
    return this.textColor(0);
};

// 经验槽颜色
Window_Base.prototype.expGaugeColor1 = function () {
    return this.textColor(30);
};

Window_Base.prototype.expGaugeColor2 = function () {
    return this.textColor(31);
};


/**
 * 
 * @param {*} color 
 * @param {*} type code:rm颜色码|hex:标准hash色号|rgb|rgba
 */
Window_Base.prototype.setNormalColor = function (color, type = 'hex') {
    if (!color) return;
    if (type === 'code') {
        this._normalColor = this.textColor(color)
    } else {
        this._normalColor = color;
    }
}

Window_Base.prototype.clearNormalColor = function () {
    this._normalColor = undefined;
}

SWDA_WindowBase.normalColor = Window_Base.prototype.normalColor
Window_Base.prototype.normalColor = function () {
    if (this._normalColor !== undefined) {
        return this._normalColor;
    }

    return SWDA_WindowBase.normalColor.call(this)
};

// 黑暗对话框颜色
Window_Base.prototype.dimColor1 = function() {
    return 'rgba(0, 0, 0, 0.45)';
};

Window_Base.prototype.dimColor2 = function() {
    return 'rgba(0, 0, 0, 0)';
};

Window_Base.prototype.dimColor3 = function() {
    return 'rgba(0, 0, 0, 0.3)';
};
// ========================================================================
// 基本字号、行高
// ========================================================================
// 较小字号（数字、参数等）
Window_Base.prototype.smallerFontSize= function() {
    return 22;
};

//标题字号
Window_Base.prototype.titleFontSize= function() {
    return 36;
};

// 设置较小字号、数字字号
Window_Base.prototype.setNumFontSize = function () {
    this.contents.fontSize = this.smallerFontSize();
};

// 设置标题字号
Window_Base.prototype.setTitleFontSize = function () {
    this.contents.fontSize = this.titleFontSize();
};

//设置标题字体、字号（中文）
Window_Base.prototype.setTitleFont= function() {
    if ($gameSystem.isChinese()) {
        this.contents.fontFace = 'hglb_cnki';
        this.contents.fontSize = this.titleFontSize();
    }
};
// 设置较小字体、字号（中文）
Window_Base.prototype.setSmallerFont= function() {
    if ($gameSystem.isChinese()) {
		this.contents.fontFace = 'msyh';
    	this.contents.fontSize = this.smallerFontSize();
	}
};

// 较小行高
Window_Base.prototype.smallerLineHeight = function() {
    return this.smallerFontSize() + 8;
};

// 标题行高
Window_Base.prototype.titleLineHeight = function() {
    return this.titleFontSize() + 8;
};

// ========================================================================
// 基本绘制
// ========================================================================
//
// 立绘绘制
Window_Base.prototype.reservePortraitImages = function () {
    $gameParty.members().forEach(function (actor) {
        ImageManager.reservePortrait(actor);
    }, this);
};

Window_Base.prototype.SWDA_drawActorPortrait = function (actor, x, y) {
    var portraitName = "Portrait" + actor.actorId() + ((actor.actorId()==1 && $gameSwitches.value(299)) ? "a" : "");
    const bitmap = ImageManager.SWDA_loadSystemPortrait(portraitName);
    if (!this._portraitSprite){
        this._portraitSprite = new Sprite_Base();
        this.addChild(this._portraitSprite);
        this._portraitSprite.x = -this.x;
        this._portraitSprite.y = -this.y;
    }
    this._portraitSprite.bitmap = bitmap;
};

// 战斗头像绘制
Window_Base.prototype.reserveBattleFaceImages = function () {
    $gameParty.members().forEach(function (actor) {
        ImageManager.reserveBattleFace(actor);
    }, this);
};


// 绘制信息数值（当前值/最大值）
//SWDA_WindowBase.drawCurrentAndMax = Window_Base.prototype.drawCurrentAndMax;
Window_Base.prototype.drawCurrentAndMax = function (current, max, x, y, width, color1, color2) {
    //SWDA_WindowBase.drawCurrentAndMax.call(this, current, max, x, y, width, color1, color2);
    //var labelWidth = this.textWidth('HP');
    var valueWidth = this.textWidth(Yanfly.Util.toGroup(max));
    var slashWidth = this.textWidth('/');
    var x1 = x + width - valueWidth;
    var x2 = x1 - slashWidth;
    var x3 = x2 - valueWidth;
    // 不再计算宽度区分是否显示分数形式，直接统一
    this.changeTextColor(color1);
    this.drawText(current, x3, y, valueWidth, 'right');
    this.changeTextColor(color2);
    this.drawText('/', x2, y, slashWidth, 'right');
    this.drawText(max, x1, y, valueWidth, 'right');

    this.resetFontSettings();
}

// 修正绘制HP槽，显示小图标
Window_Base.prototype.drawActorHp = function (actor, x, y, width) {
    width = width || 186;
    const color1 = this.hpGaugeColor1();
    const color2 = this.hpGaugeColor2();
    this.drawGauge(x, y - 8, width, actor.hpRate(), color1, color2);
    this.drawIcon(160, x, y - 2);
    this.drawCurrentAndMax(actor.hp, actor.mhp, x, y - 6, width,
        this.hpColor(actor), this.normalColor());
};
// 修正绘制MP槽，显示小图标
Window_Base.prototype.drawActorMp = function (actor, x, y, width) {
    width = width || 186;
    const color1 = this.mpGaugeColor1();
    const color2 = this.mpGaugeColor2();
    this.drawGauge(x, y - 8, width, actor.mpRate(), color1, color2);
    this.drawIcon(161, x, y - 2);
    this.drawCurrentAndMax(actor.mp, actor.mmp, x, y - 6, width,
        this.mpColor(actor), this.normalColor());
};

// 修正绘制TP槽的长度，显示小图标
Window_Base.prototype.drawActorTp = function (actor, x, y, width) {
    width = width || 186;
    let color1 = this.tpGaugeColor1();
    let color2 = this.tpGaugeColor2();
    this.drawGauge(x, y - 8, width, actor.tpRate(), color1, color2);
    this.drawIcon(162, x, y - 2);
    this.drawCurrentAndMax(actor.tp, actor.maxTp(), x, y - 6, width,
        this.mpColor(actor), this.normalColor());
};

// 绘制角色等级
Window_Base.prototype.drawActorLevel = function (actor, x, y) {
    this.resetTextColor();
    this.setSmallerFont();
    this.drawText(actor.level, x, y, 32, 'right');
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.levelA, x + 32, y, 28, 'right');
    this.resetFontSettings();

};

// 绘制垂直字
Window_Base.prototype.drawVerticalText = function (text, x, y, width, lineHeight, align = 'center') {
    for (let i = 0; i < text.length; ++i) {
        const word = text[i]
        this.drawText(word, x, y + lineHeight * i, width, align)
    }
}

// 绘制物品名（去除括号内文字）
Window_Base.prototype.drawItemName = function(item, wx, wy, ww) {
    ww = ww || 312;
    this.setItemTextColor(item);

    if (item) {
        const iconBoxWidth = Window_Base._iconWidth + 4;
        this.resetTextColor();
        this.drawIcon(item.iconIndex, wx + 2, wy + 2);
        this.drawText(item.name.split("（")[0], wx + iconBoxWidth, wy, ww - iconBoxWidth);
    }
    //Yanfly.Item.Window_Base_drawItemName.call(this, item, wx, wy, ww);
    this._resetTextColor = undefined;
    this.resetTextColor();
};

//法宝经验槽
Window_Base.prototype.drawItemExp = function (item, x, y, width) {
    this.resetTextColor();
    let exp = item.exp();
    let maxExp = item.nextLevelExp();
    let rate = exp / maxExp;
    if (item.level() === item.maxLevel())
        rate = 1;
    let color1 = this.expGaugeColor1();
    let color2 = this.expGaugeColor2();
    this.drawGauge(x, y, width, rate, color1, color2);
    this.contents.fontSize = 18;
    let expTextWidth = this.textWidth(exp + '/' + maxExp);
    this.drawText(exp + '/' + maxExp, x + width + 4, y + 8, expTextWidth);
    //this.drawCurrentAndMax(exp, maxExp, x+width , y, expTextWidth, this.normalColor(), this.normalColor());
    this.contents.fontSize = this.standardFontSize();
};


// 绘制种族：种族图标序号
Window_Base.prototype.enemyClassIcon = function (enemyClassId) {
    let enemyClassIcon = 240;   //魔王
    if (enemyClassId >= 6 && enemyClassId <= 22) {
        enemyClassIcon = enemyClassId - 6 + 224;
    }
    return enemyClassIcon;
};
// 绘制种族：种族图标及文字{}
Window_Base.prototype.enemyClassText = function (enemyClassId) {
    let classText=[];
    classText[0] = '\\i[' + this.enemyClassIcon(enemyClassId) + ']' ;
    if (enemyClassId >= 6 && enemyClassId <= 22) {
        classText[1] = '[' + $dataClasses[enemyClassId].name + ']';
    } else {
        classText[1] = '';
    }
    return classText;
};

// 绘制属性名
Window_Base.prototype.drawElementName = function (eleId, dx, dy) {
    const eleName = $dataSystem.elements[eleId];
    this.changeTextColor(this.systemColor());
    this.drawText(eleName, dx, dy);
    this.resetTextColor();
};
// 绘制属性抗性
Window_Base.prototype.drawElementData = function (actor, eleId, dx, dy) {
    let eleRate = actor.elementRate(eleId);

    let eleText = '';
    let text = eleText;
    let eleColor = '#65cbff'

    if (eleRate > 1) {
        eleText = '损 ';
        eleRate -= 1;
        eleColor = '#ff4d00'
    } else if (eleRate === 1) {
        eleText = '—';
        eleColor = '#fbdb45'
    } else if (eleRate >= 0) {
        eleText = '抗 ';
        eleRate -= 1;
        eleColor = '#59ba8e'
    } else {
        eleText = '吸 ';
        eleColor = '#65cbff'
    }

    //dx += this.textPadding();
    //dw -= this.textPadding() * 2;

    if (eleRate === 1) {
        text = eleText;
    } else {
        text = eleText + Math.abs(Math.round(eleRate * 100)).toFixed(Yanfly.Param.StatusEleDec) + '%';
    }
    this.changeTextColor(eleColor);
    this.drawText(text, dx, dy);
    this.resetTextColor();
};
// 选项无效时的颜色（原版为改透明度，现改为红色显示）
Window_Base.prototype.changePaintOpacity = function(enabled) {
    //this.contents.paintOpacity = enabled ? 255 : this.translucentOpacity();
    if(!enabled)
        this.changeTextColor(this.hpGaugeColor1());
    //else
    //    this.resetTextColor();
};